﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Text;

public partial class Member_UC_AccountSetting : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ControlsInit();
        }
    }

    protected void ControlsInit()
    {
        //初始化初始化已绑定微博列表
        string sql = "select id,WbType,WbName, WeiboUserId from weibo where userId=1";
        DataTable dt = eSYSU.DAL.SqlHelper.GetDataTable(sql);
        r_WeiboAccountList.DataSource = dt;
        r_WeiboAccountList.DataBind();

        //初始化学院列表
        dt = eSYSU.Business.CollegeService.GetColleges(0);
        ddl_College.DataSource = dt;
        ddl_College.DataValueField = "ID";
        ddl_College.DataTextField = "Name";
        ddl_College.DataBind();

        //初始化新闻订阅列表
        lb_NewsCategory.DataSource = dt;  //这里的dt用到了上边的查询结果，因此代码的位置不能改变
        lb_NewsCategory.DataValueField = "ID";
        lb_NewsCategory.DataTextField = "Name";
        lb_NewsCategory.DataBind();
        sql = "select categoryList from subscription where userid = 1";
        dt = eSYSU.DAL.SqlHelper.GetDataTable(sql);
        if(dt.Rows.Count>0)
        {
            if (dt.Rows[0][0].ToString().Equals("0"))    //订阅全部新闻的情形
            {
                for (int i = 0; i < lb_NewsCategory.Items.Count; i++)
                {
                    lb_NewsCategory.Items[i].Selected = true;
                }
            }
            else
            {
                string[] college = dt.Rows[0][0].ToString().Trim().Split(new char[] { ' ' });
                foreach (string s in college)
                {
                    lb_NewsCategory.Items.FindByValue(s).Selected = true;
                    //Response.Write(s);
                }
            }
        }

        //地址下拉列表
        ddl_Province.DataSource = eSYSU.DAL.SqlHelper.GetDataTable("select CityID,CityName from City where RootID = 0");
        ddl_Province.DataValueField = "CityID";
        ddl_Province.DataTextField = "CityName";
        ddl_Province.DataBind();
        ddl_City.DataSource = eSYSU.DAL.SqlHelper.GetDataTable("select CityID,CityName from City where RootID = 1");
        ddl_City.DataValueField = "CityID";
        ddl_City.DataTextField = "CityName";
        ddl_City.DataBind();

        //日期下拉列表
        for (int i = 2000; i > 1920; i--)
        {
            ddl_Birthday_Year.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
    }

    protected void btn_SaveBasic_Click(object sender, EventArgs e)
    {

    }
    protected void btn_SaveSubs_Click(object sender, EventArgs e)
    {
        StringBuilder sSubList = new StringBuilder();
        if (cb_SubAll.Checked)  //订阅全部新闻
        {
            sSubList.Append(0);
        }
        else
        {
            int[] subList = lb_NewsCategory.GetSelectedIndices();
            foreach (int i in subList)
            {
                sSubList.Append(lb_NewsCategory.Items[i].Value + " ");
            }
        }

        IDictionary<string, object> parmsDic = new Dictionary<string, object>();
        parmsDic.Add("@_UserId", 1);
        parmsDic.Add("@_CategoryList", sSubList.ToString());
        eSYSU.DAL.SqlHelper.ExecuteNonQuery(eSYSU.DAL.SqlHelper.ConnString, CommandType.StoredProcedure, "proc_Subscription_Update", parmsDic);
    }


    protected void btn_SaveVerify_Click(object sender, EventArgs e)
    {

    }


    protected void btn_AddWeiboAccount_Click(object sender, EventArgs e)
    {
        if (rbl_WeiboType.SelectedValue.Equals("sina"))
        {
            oAuthSina oauth = new oAuthSina();
            oauth.RequestTokenGet();
            string url = oauth.AuthorizationGet();
            //Session["oauth_token"] = oauth.token;
            //Session["oauth_token_secret"] = oauth.tokenSecret;

            Response.Redirect(url + "&oauth_callback=http://localhost/WeiboOauth.aspx?e_token=" + oauth.token + "-" + oauth.tokenSecret);
        }
        else
        {
            //腾讯微博认证
        }
    }


    protected void ddl_Province_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddl_City.DataSource = eSYSU.DAL.SqlHelper.GetDataTable(String.Format("select cityid,CityName from City where rootid={0}", ddl_Province.SelectedValue));
        ddl_City.DataValueField = "cityid";
        ddl_City.DataTextField = "cityname";
        ddl_City.DataBind();
    }
}